<template>
  <div class="upload-item" :style="{width: width+'rem',height:height+'rem'}" :class="{'no-photo':!hasPhoto}">
    <div class="upload-no-photo" v-show="!hasPhoto">
      <input type="file" accept="image/*" @change="onFileChange">
      <div>
        <img src="../assets/upload-icon.png" alt="">
      </div>
      <div class="upload-tip-text">{{tipText}}</div>
    </div>
    <div class="upload-has-photo" v-show="hasPhoto" :style="{'background-image': 'url('+imgData+')'}">
      <div class="upload-delete-btn" @click="onDelete">
        <img src="../assets/delete-icon.png" alt="">
        <span>删除</span>
      </div>
    </div>
  </div>
</template>

<script>
import compress from './image'
export default {
  props: {
    width: { // 上传组件的宽度 以rem为单位
      type: Number,
      default: 2.1
    },
    height: {
      type: Number,
      default: 2.1
    },
    tipText: { // 上传图片的提示文字
      type: String,
      default: ''
    },
    compressWidth: { // 压缩图片的宽度 以px为单位
      type: Number,
      default: 1000
    },
    compressHeight: {
      type: Number,
      default: 1000
    },
    compressQuality: { // 压缩图片的质量
      type: Number,
      default: 0.92
    }
  },
  data() {
    return {
      hasPhoto: false,
      imgData: '' // 得到的图片的base64字符串
    }
  },
  methods: {
    onDelete() {
      this.hasPhoto = false
      this.imgData = ''
    },
    onFileChange(e) {
      const fileEle = e.currentTarget
      const files = fileEle.files
      if (files && files[0]) {
        const file = files[0]
        if (file.size > 1024 * 1024) {
          compress(file, {
            compress: {
              width: this.compressWidth,
              height: this.compressHeight,
              quality: this.compressQuality
            }
          }, this.compressCallBack)
        } else {
          compress(file, { compress: false }, this.compressCallBack)
        }
        // 解决文件重复选择的问题
        fileEle.value = ''
      }
    },
    compressCallBack(file) {
      this.hasPhoto = true
      this.imgData = file.base64
    }
  }
}
</script>

<style scoped lang="scss">
.upload-item {
  position: relative;
  display: inline-flex;
  justify-content: center;
  align-items: center;
  margin: auto;
  background: #fbfbfb center no-repeat;
  background-size: cover;
  border: 2px solid #d4d4d4;
  &.no-photo {
    border: 2px dashed #d4d4d4;
  }
  .upload-no-photo {
    text-align: center;
    width: 100%;
    img {
      height: 0.5rem;
      width: auto;
    }
    .upload-tip-text {
      width: 90%;
      margin: 0.05rem auto 0;
      font-size: 0.24rem;
      color: #999;
      white-space: wrap;
    }
  }
  .upload-has-photo {
    width: 100%;
    height: 100%;
    background: center no-repeat;
    background-size: cover;
    .upload-delete-btn {
      width: 100%;
      height: 0.7rem;
      line-height: 0.7rem;
      text-align: center;
      position: absolute;
      left: 0;
      bottom: 0;
      background-color: rgba(0, 0, 0, 0.4);
      cursor: pointer;
      img {
        width: auto;
        height: 0.3rem;
        vertical-align: middle;
      }
      span {
        vertical-align: middle;
        font-size: 0.28rem;
        color: #fff;
        margin-left: 0.1rem;
      }
    }
  }
  input {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    width: 100%;
    font-size: 0;
    opacity: 0;
  }
}
</style>
